/**
 * Created by cxj on 2016/2/22.
 */


var bootbox_global;
var grid;
//删除角色名
function deleteRole(roleid)
{

    bootbox= bootbox_global;
    // alert(roleid);

    bootbox_global.dialog({
        message: "确定要删除吗?",
        buttons: {
            Cancel: {
                label: "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                callback: function () {
                }
            }
            , OK: {
                label: "<i class='icon-ok'></i> 确定",
                "className": "btn-sm btn-success",
                callback: function () {


                    //发送请求
                    send_request_direct("/workerType/delete", "obj_id="+roleid, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });

                }
            }
        }
    });

}

//修改角色名
function updateRole(type_id)
{
    bootbox= bootbox_global;

    //alert(roleid);

    var alertarray = {
        form: {api: "/workerType/update", name: "alertform", id: "alertform"},
        inputs: [
            {title: "工种名", type: "text", placeholder: "", verify: "empty", name: "name", id: "name"}
        ]

    }
    bootbox.dialog({
        title: "修改",
        message: generateAlertForm(alertarray),
        buttons: {
            "cancel": {
                "label": "<i class='icon-info'></i> 取消",
                "className": "btn-sm btn-danger",
                "callback": function () {
                }
            },
            "success": {
                "label": "<i class='icon-ok'></i> 保存",
                "className": "btn-sm btn-success",
                "callback": function () {

                    //验证
                    if (check_form_info_all(alertarray.form.name) == false)
                        return false;

                    //发送请求
                    ajax_request(alertarray.form.name, "&obj_id="+type_id, function process_data(data) {


                        //操作成功
                        AlertSuccess(bootbox, "操作成功", function () {

                            grid.Init();


                        });


                    }, function process_fail(data) {

                        //操作失败
                        AlertError(bootbox, data.msg);


                    });


                }
            }
        }
    });

}

//权限设置
function resetObj(roleid,index)
{

    window.location = '/role/authlist?role_id='+roleid;

}
define([
    "jquery",
    "IO",
    "bootbox"
], function($,IO,bootbox) {
    $(function () {

        bootbox_global = bootbox;



        //每列数据列性
        var columns = [

            {title : '编号',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                return "<span>"+parseInt( (gridobj.current_page-1)*gridobj.page_rows+parseInt(datarow)+1)+"</span>";
            }},
            //{ title: '角色id', dataIndex: 'role_id',},
            { title: '工种', dataIndex: 'name',},
            { title: '创建时间', dataIndex: 'create_time',},
            {title : '操作',dataIndex :'User_ID',dataSource:function(data,datarow,gridobj,current_column) {

                var html= "<div class='div_btn'><button onclick=\"updateRole("+data[datarow].type_id+")\" class='btn btn-info'>修改</button> <button onclick=\"deleteRole("+data[datarow].type_id+")\"  class='btn btn-danger'>删除</button>";



                html = html+"</div>"

                return html;
            }},
            //{ title: '修改时间', dataIndex: 'update_time',},


        ];


        //数组初始化

        function dataInit() {
            grid = new Grid("/workerType/getListPaging", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 20);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {

            //var param="mobile="+$("#mobile").val();

            //return param;

            return "";
        }
        dataInit();
        //查询
        $("#query").bind("click", function () {
            dataInit();
        });


        //新增角色
        $("#btn_add").bind("click",function() {


            var alertarray = {
                form: {api: "/workerType/add", name: "alertform", id: "alertform"},
                inputs: [
                    {title: "工种名", type: "text", placeholder: "", verify: "empty", name: "name", id: "name"}
                ]

            }
            bootbox.dialog({
                title: "新增",
                message: generateAlertForm(alertarray),
                buttons: {
                    "cancel": {
                        "label": "<i class='icon-info'></i> 取消",
                        "className": "btn-sm btn-danger",
                        "callback": function () {
                        }
                    },
                    "success": {
                        "label": "<i class='icon-ok'></i> 保存",
                        "className": "btn-sm btn-success",
                        "callback": function () {

                            //验证
                            if (check_form_info_all(alertarray.form.name) == false)
                                return false;

                            //发送请求
                            ajax_request(alertarray.form.name, "", function process_data(data) {


                                //操作成功
                                AlertSuccess(bootbox, "操作成功", function () {

                                    grid.Init();


                                });


                            }, function process_fail(data) {

                                //操作失败
                                AlertError(bootbox, data.msg);


                            });


                        }
                    }
                }
            });


        });
    });


});